package com.woniu.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.exception.LXLGException;
import com.woniu.product.common.CouponResultCode;
import com.woniu.product.common.enums.CouponReceiveStatus;
import com.woniu.product.infra.model.Coupon;
import com.woniu.product.infra.model.CouponReceive;
import com.woniu.product.infra.mapper.CouponReceiveMapper;
import com.woniu.product.service.CouponReceiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.product.service.CouponService;
import com.woniu.product.service.dto.ReceiveRecordDto;
import com.woniu.product.service.form.ReceiveCouponServiceParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券领取记录表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-11-26
 */
@Service
public class CouponReceiveServiceImpl extends ServiceImpl<CouponReceiveMapper, CouponReceive> implements CouponReceiveService {


    @Resource
    private CouponService couponService;

    @Override
    public List<ReceiveRecordDto> receiveRecord(Integer userId, String state) {
        //查看用户的优惠券
        List<CouponReceive> allRecord = null;
        if (state.equals("") || state == null) {
            //查看所有
            QueryWrapper<CouponReceive> qw = new QueryWrapper<>();
            qw.eq("buyer_id", userId);
            allRecord = list(qw);
            System.out.println("qqqqqqq");
        } else {
            QueryWrapper<CouponReceive> qw = new QueryWrapper<>();
            qw.eq("buyer_id", userId);
            qw.eq("status", state);
            allRecord = list(qw);
        }
        List<ReceiveRecordDto> receiveRecordDtos = BeanUtil.copyToList(allRecord, ReceiveRecordDto.class);
        return receiveRecordDtos;
    }

    /**
     * 修改领取的优惠券状态
     *
     * @param receiveRecordId
     */
    @Override
    public void updateReceiveRecord(Integer receiveRecordId) {
        CouponReceive couponReceive = getById(receiveRecordId);
        if (couponReceive == null) {
            throw new LXLGException(CouponResultCode.COUPONRECEIVE_NOT_FOUND);
        }
        couponReceive.setStatus(CouponReceiveStatus.USED.getCode());
        updateById(couponReceive);
    }

    /**
     * 领取优惠券
     *
     * @param receiveCouponServiceParam
     */
    @Override
    public void receiveCoupon(ReceiveCouponServiceParam receiveCouponServiceParam) {
        Coupon coupon = couponService.getById(receiveCouponServiceParam.getCouponId());
        if (coupon == null) {
            throw new LXLGException(CouponResultCode.COUPON_NOT_FOUND);
        }
        if (coupon.getTakeCount() >= coupon.getQuota()) {
            throw new LXLGException(CouponResultCode.COUPON_INSUFFICE);
        }
        CouponReceive couponReceive = new CouponReceive();
        couponReceive.setBuyerId(receiveCouponServiceParam.getUserId());
        couponReceive.setCouponId(receiveCouponServiceParam.getCouponId());
        couponReceive.setCouponMoney(coupon.getUsedAmount());
        couponReceive.setCreateTime(new Date());
        couponReceive.setFullMoney(coupon.getWithAmount());
        couponReceive.setStatus(CouponReceiveStatus.UNUSED.getCode());
        save(couponReceive);
        coupon.setTakeCount(coupon.getTakeCount() + 1);
        couponService.updateById(coupon);
    }
}

